package com.dao;

import com.pojo.User;
import com.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    static Connection conn = null; // 连接对象
    static PreparedStatement ps = null; // 预编译
    static ResultSet rs = null; // 结果集

    /**
     * 功能 断用户名是否存在
     * 参数 userName
     * 返回值 boolean 存在返回 true，不存在返回 flase
     */
    public boolean checkUser(String userName) {
        boolean result = false;
        try {
            conn = DBConnection.getConnection();
            String sql = "select * from t_user where userName = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("用户名已存在");
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 功能 注册用户
     * 参数 user
     * 返回值 boolean 注册返回 true，失败返回 flase
     */
    public boolean register(User user) {
        boolean result = false;
        boolean b = checkUser(user.getUserName());
        if (!b) {
            try {
                conn = DBConnection.getConnection();
                String sql = "insert into t_user (userName,passWord,phone) values (?,?,?)";
                ps = conn.prepareStatement(sql);
                ps.setString(1, user.getUserName());
                ps.setString(2, user.getPassWord());
                ps.setString(3, user.getPhone());
                int i = ps.executeUpdate();
                if (i == 1) {
                    result = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 功能 用户登录验证
     * 参数 user
     * 返回值 User 对象
     */

    public User login(String userName, String passWord) {
        boolean result = false;
        User user = new User();
        try {
            conn = DBConnection.getConnection();
            String sql = "select * from t_user where userName = ? and passWord = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, passWord);
            rs = ps.executeQuery();
            if (rs.next()) {
                user.setUserId(rs.getInt("userId"));
                user.setPhone(rs.getString("phone"));
                user.setUserName(userName);
                user.setPassWord(passWord);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
    /********** Begin **********/

    /**
     * 功能 注销自己的账号
     * 参数 user
     */
    public void deleteUser(User user) {
        int blogId[]=new int[100];
        BlogDao blogDao=new BlogDao();
        CommentDao commentDao=new CommentDao();
        // 删除自己发布的评论和删除自己博客中的评论
        try {
            conn = DBConnection.getConnection();
            String sql = "select t_blog.blogId from t_blog where t_blog.userId = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, user.getUserId());
            rs = ps.executeQuery();
            int i=0;
            while (rs.next()) {
                blogId[i]=rs.getInt("blogId");
                i++;
            }
            String sql1 = "delete from t_comment where t_comment.userId = ?";
            PreparedStatement ps1 = conn.prepareStatement(sql1);
            ps1.setInt(1, user.getUserId());
            ps1.executeUpdate();
            String sql3 = "delete from t_comment where t_comment.blogId = ?";
            PreparedStatement ps4 = conn.prepareStatement(sql3);
            for(int blogid:blogId){
                ps4.setInt(1, blogid);
                ps4.executeUpdate();
            }
            //删除博客
            String sql4 = "delete from t_blog where t_blog.blogId = ?";
            PreparedStatement ps2 = conn.prepareStatement(sql4);
            for(int blogid:blogId){
                ps2.setInt(1, blogid);
                ps2.executeUpdate();
            }
            // 删除账号
            String sql2 = "delete from t_user where t_user.userId = ?";
            PreparedStatement ps3 = conn.prepareStatement(sql2);
            ps3.setInt(1, user.getUserId());
            ps3.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    /********** End **********/
}